package com.yifeng.repo.base.service;

import com.gomcarter.frameworks.base.common.AssertUtils;
import com.gomcarter.frameworks.base.exception.CustomException;
import com.gomcarter.frameworks.base.json.ErrorCode;
import com.yifeng.base.bds.api.lbs.LbsRestService;
import com.yifeng.base.bds.api.store.StoreLocationRestService;
import com.yifeng.base.mcs.api.EventNotifyRestService;
import com.yifeng.base.mcs.api.MqttRestService;
import com.yifeng.base.mcs.api.MsgSendRestService;
import com.yifeng.base.mcs.api.VoiceRestService;
import com.yifeng.base.mqtt.api.MqttMsgRestService;
import com.yifeng.repo.base.params.ConsoleParam;
import com.yifeng.repo.base.security.proxy.CommonProxy;
import com.yifeng.repo.base.service.constant.BaseServiceTypeEnum;
import com.yifeng.repo.base.service.impl.*;
import com.yifeng.repo.base.utils.common.BaseUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Created by daibing on 2024/2/3.
 */
@Slf4j
public class BaseServiceManager {
    protected final ConcurrentMap<BaseServiceTypeEnum, CommonProxy> baseServiceType2Proxy = new ConcurrentHashMap<>();
    private final ConsoleParam bdsConsoleParam;
    private final ConsoleParam mcsConsoleParam;
    private final ConsoleParam mqttConsoleParam;

    public BaseServiceManager(ConsoleParam bdsConsoleParam, ConsoleParam mcsConsoleParam) {
        this.bdsConsoleParam = bdsConsoleParam;
        this.mcsConsoleParam = mcsConsoleParam;
        this.mqttConsoleParam = null;
    }

    public BaseServiceManager(ConsoleParam bdsConsoleParam, ConsoleParam mcsConsoleParam, ConsoleParam mqttConsoleParam) {
        this.bdsConsoleParam = bdsConsoleParam;
        this.mcsConsoleParam = mcsConsoleParam;
        this.mqttConsoleParam = mqttConsoleParam;
    }

    public void init() {
        log.info("yfcloud BaseServiceManager init success: OWNER_ID={}, OWNER_NAME={}", BaseUtil.OWNER_ID, BaseUtil.OWNER_NAME);
    }

    public void destroy() {
        log.info("yfcloud BaseServiceManager destroy success: OWNER_ID={}, OWNER_NAME={}", BaseUtil.OWNER_ID, BaseUtil.OWNER_NAME);
    }

    public LbsRestService getLbsRestService() {
        AssertUtils.notNull(bdsConsoleParam, new CustomException(ErrorCode.paramError));
        baseServiceType2Proxy.computeIfAbsent(BaseServiceTypeEnum.LBS, k -> new LbsRestServiceImpl(bdsConsoleParam));
        return (LbsRestService) baseServiceType2Proxy.get(BaseServiceTypeEnum.LBS);
    }

    public MsgSendRestService getMsgSendRestService() {
        AssertUtils.notNull(mcsConsoleParam, new CustomException(ErrorCode.paramError));
        baseServiceType2Proxy.computeIfAbsent(BaseServiceTypeEnum.MSG_SEND, k -> new MsgSendRestServiceImpl(mcsConsoleParam));
        return (MsgSendRestService) baseServiceType2Proxy.get(BaseServiceTypeEnum.MSG_SEND);
    }

    public EventNotifyRestService getEventNotifyRestService() {
        AssertUtils.notNull(mcsConsoleParam, new CustomException(ErrorCode.paramError));
        baseServiceType2Proxy.computeIfAbsent(BaseServiceTypeEnum.EVENT_NOTIFY, k -> new EventNotifyRestServiceImpl(mcsConsoleParam));
        return (EventNotifyRestService) baseServiceType2Proxy.get(BaseServiceTypeEnum.EVENT_NOTIFY);
    }

    public StoreLocationRestService getStoreLocationRestService() {
        AssertUtils.notNull(bdsConsoleParam, new CustomException(ErrorCode.paramError));
        baseServiceType2Proxy.computeIfAbsent(BaseServiceTypeEnum.STORE_LOCATION, k -> new StoreLocationRestServiceImpl(bdsConsoleParam));
        return (StoreLocationRestService) baseServiceType2Proxy.get(BaseServiceTypeEnum.STORE_LOCATION);
    }

    public VoiceRestService getVoiceRestService() {
        AssertUtils.notNull(mcsConsoleParam, new CustomException(ErrorCode.paramError));
        baseServiceType2Proxy.computeIfAbsent(BaseServiceTypeEnum.VOICE, k -> new VoiceRestServiceImpl(mcsConsoleParam));
        return (VoiceRestService) baseServiceType2Proxy.get(BaseServiceTypeEnum.VOICE);
    }

    public MqttRestService getMqttRestService() {
        AssertUtils.notNull(mcsConsoleParam, new CustomException(ErrorCode.paramError));
        baseServiceType2Proxy.computeIfAbsent(BaseServiceTypeEnum.MCS_MQTT, k -> new MqttRestServiceImpl(mcsConsoleParam));
        return (MqttRestServiceImpl) baseServiceType2Proxy.get(BaseServiceTypeEnum.MCS_MQTT);
    }

    public MqttMsgRestService getMqttMsgRestService() {
        AssertUtils.notNull(mqttConsoleParam, new CustomException(ErrorCode.paramError));
        baseServiceType2Proxy.computeIfAbsent(BaseServiceTypeEnum.MQTT, k -> new MqttMsgRestServiceImpl(mqttConsoleParam));
        return (MqttMsgRestService) baseServiceType2Proxy.get(BaseServiceTypeEnum.MQTT);
    }
}
